#include "psglmodel.h"

PSGLModel::PSGLModel()
{
}

void PSGLModel::renderFrame(GLint frameNumber, bool draw_planet, bool draw_tr, bool draw_text)
{
  if (draw_planet)
  {
    for (QList<PSGLObject>::iterator it = objects.begin(); it != objects.end(); it++)
    {
      (*it).render(frameNumber);
    }
  }
  if(draw_tr)
  {
      for (QList<PSGLObject>::iterator it = objects.begin(); it != objects.end(); it++)
      {
          (*it).renderTrajectory();
      }
  }

  if(draw_text)
  {
      for (QList<PSGLObject>::iterator it = objects.begin(); it != objects.end(); it++)
      {
          (*it).renderText(frameNumber);
      }
  }

  renderBonds(frameNumber);
  renderRopes(frameNumber);
}

void PSGLModel::renderBonds(GLint frameNumber)
{

  glLineWidth(3.0f);
  int  max = objects.size();
  for (int i = 0; i < max; i++)
  {
    for (int j = 0; j < max; j++)
    {
      if (bonds[i][j] != 0)
      {
        Point3D p1 = objects[i].trajectory[frameNumber];
        Point3D p2 = objects[j].trajectory[frameNumber];


        glColor4f(objects[i].cr, objects[i].cg, objects[i].cb, objects[i].ca);
        glBegin(GL_LINES);
          glVertex3f( p1.x, p1.y,  p1.z);
          glVertex3f((p1.x + p2.x)/2, (p1.y + p2.y)/2,  (p1.z + p2.z)/2);
        glEnd();

        glColor4f(objects[j].cr, objects[j].cg, objects[j].cb, objects[j].ca);
        glBegin(GL_LINES);
          glVertex3f((p1.x + p2.x)/2, (p1.y + p2.y)/2,  (p1.z + p2.z)/2);
          glVertex3f(p2.x, p2.y,  p2.z);
        glEnd();
      }
    }
  }
  glLineWidth(1.0f);
}


void PSGLModel::renderRopes(GLint frameNumber)
{

  glLineWidth(2.0f);
  int  max = objects.size();
  for (int i = 0; i < max; i++)
  {
    for (int j = 0; j < max; j++)
    {
      if (ropes[i][j] != 0)
      {
        Point3D p1 = objects[i].trajectory[frameNumber];
        Point3D p2 = objects[j].trajectory[frameNumber];
        Point3D vector = p2 - p1;

        for(int k = 0; k < 5; k++)
        {

        glColor4f(objects[i].cr, objects[i].cg, objects[i].cb, objects[i].ca);
        glBegin(GL_LINES);
          glVertex3f( p1.x + 0.025*(4*k)*vector.x , p1.y + 0.025*(4*k)*vector.y,  p1.z + 0.025*(4*k)*vector.z);
          glVertex3f( p1.x + 0.025*(4*k + 1)*vector.x , p1.y + 0.025*(4*k + 1)*vector.y,  p1.z + 0.025*(4*k + 1)*vector.z);
        glEnd();

        //glColor4f(objects[j].cr, objects[j].cg, objects[j].cb, objects[j].ca);
        glBegin(GL_LINES);
          glVertex3f( p1.x + 0.025*(4*k + 2)*vector.x , p1.y + 0.025*(4*k + 2)*vector.y,  p1.z + 0.025*(4*k + 2)*vector.z);
          glVertex3f( p1.x + 0.025*(4*k + 3)*vector.x , p1.y + 0.025*(4*k + 3)*vector.y,  p1.z + 0.025*(4*k + 3)*vector.z);
        glEnd();
        }
      }
    }
  }
  glLineWidth(1.0f);
}


void PSGLModel::addObject(PSGLObject newObject)
{
    objects.push_back(newObject);
}

void PSGLModel::deleteObjectByName(QString p_name)
{
    int i = 0;
    for (QList<PSGLObject>::iterator it = objects.begin(); it != objects.end(); it++, i++)
    {
        if ((*it).name == p_name)
            objects.removeAt(i);
    }

}
